home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / tcoop.arc / TCOOP2.ARC / GSCROLL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-26  |  7.0 KB  |  251 lines

  1. // gscroll.cpp: Graphics Scroll Window Class Implementations
  2.  
  3. #include "math.h"
  4. #include "gscroll.h"
  5.  
  6. // ----------------------  Slider Type ------------------- 
  7.  
  8. Slider::Slider(ColorPak &Cp)
  9. // Initialize the object that represents the slider 
  10. : Wso(Relief+BorderWd, Swappable, Cp)
  11. {
  12.   return;
  13. }
  14.  
  15. void Slider::OnMouseEnter(MsgPkt &M)
  16. // Changes the mouse cursor when it is on the slider 
  17. {
  18.   Wso::OnMouseEnter(M);      // Generally call this first 
  19.   if (((ScrollBar *)Base)->Orientation == HzOrient)
  20.      Mouse.SetGCursor(LeftRightCursor);
  21.      else Mouse.SetGCursor(UpDownCursor);
  22. }
  23.  
  24. void Slider::OnMouseLeave(MsgPkt &M)
  25. // Restores the mouse cursor when the mouse leaves the slider 
  26. {
  27.   Mouse.SetGCursor(ArrowCursor);
  28.   Wso::OnMouseLeave(M);
  29. }
  30.  
  31. void Slider::OnMouseDown(MsgPkt &M)
  32. // Moves the slider when the mouse is anywhere within the slider 
  33. {
  34.   SwitchFocus(M);
  35.   BorderHandler(M);
  36. }
  37.  
  38. void Slider::Move(int X, int Y)
  39. // Moves the slider to a new absolute coordinate 
  40. {
  41.   Wso::Move(X, Y);
  42.   ((ScrollBar *)Base)->SendScrollPosn();
  43. }
  44.  
  45. void Slider::OnKeyStroke(MsgPkt &M)
  46. // Sends keystrokes to slider bar 
  47. {
  48.   Base->OnKeyStroke(M);
  49. }
  50.  
  51. // --------------------- ScrollBar Type -------------------- 
  52.  
  53. ScrollBar::ScrollBar(BarOrient Orient, ColorPak &Cp)
  54. // Initializes a scroll bar object with a slider within it 
  55. : Wso(Recessed+BorderWd, 0x00, Cp)
  56. {
  57.   Orientation = Orient;
  58.   Slide = new Slider(Cp);
  59.   // Allow slider to overlap with the frame of the scroll bar 
  60.   Slide->ClipToFrame = True;
  61.   InhibitMessage = False;  
  62. }
  63.  
  64. void ScrollBar::SetSize(int W, int H)
  65. // Sets the size of the scroll bar and slider dep}ing 
  66. // on whether it is horizontally or vertically oriented 
  67. {
  68.   int D;
  69.   Wso::SetSize(W,H);
  70.   if (Orientation == HzOrient)
  71.     D = Panel->Frame->Ht - Panel->Bwd * 2;
  72.     else D = Panel->Frame->Wd - Panel->Bwd * 2;
  73.   Slide->SetSize(D, D);
  74. }
  75.  
  76. void ScrollBar::Open(Iso *B, int X, int Y)
  77. // Opens the scroll bar and the slider within it 
  78. {
  79.   Wso::Open(B, X, Y);
  80.   Slide->Open(this, 0, 0);
  81. }
  82.  
  83. void ScrollBar::Redraw(void)
  84. // Redraw the bar and the slider 
  85. {
  86.   Wso::Redraw();
  87.   Slide->Redraw();
  88. }
  89.  
  90. void ScrollBar::OnMouseEnter(MsgPkt &M)
  91. // Changes the mouse cursor when it is over the scroll bar 
  92. {
  93.   Wso::OnMouseEnter(M); // Generally call this first 
  94.   Mouse.SetGCursor(HandCursor);
  95. }
  96.  
  97. void ScrollBar::OnMouseLeave(MsgPkt &M)
  98. // Restores the mouse cursor when it leaves the scroll bar 
  99. {
  100.   Mouse.SetGCursor(ArrowCursor);
  101.   Wso::OnMouseLeave(M); // Generally call this last 
  102. }
  103.  
  104. void ScrollBar::OnMouseDown(MsgPkt &M)
  105. // Moves the slider to the location where the mouse was just pressed 
  106. {
  107.   if (Panel->OnInterior(M.Mx, M.My)) {
  108.      Slide->Move(M.Mx, M.My); 
  109.      // Need to do this so you can leave the mouse button down
  110.      // and still move 
  111.      Slide->OnMouseDown(M);   
  112.   }
  113. }
  114.  
  115. void ScrollBar::SendScrollPosn(void)
  116. // Sends a message to the base window to report the current 
  117. // location of the slider 
  118. {
  119.   int Ip;
  120.   float Rp;
  121.   MsgPkt M;
  122.   if (InhibitMessage) return;
  123.   if (Orientation == HzOrient) {
  124.     // Compute relative position of slider in slidebar 
  125.     Ip = Slide->Panel->Interior->Xul - Panel->Interior->Xul; 
  126.     if (Ip == 0) {
  127.        Rp = 0.0;
  128.     }
  129.     else {
  130.       int X = Panel->Interior->Wd-1-(Slide->Panel->Overall->Wd-1) / 2;
  131.       Rp = float(Ip) / float(X);
  132.     }
  133.     // Send a message to base to update according to the new
  134.     // location of the slider 
  135.     M.RtnCode = HzScrollMove;  M.Code = HzScrollMove;
  136.     M.Focus = Base; // Send message to base 
  137.     M.Mx = floor(Rp * 10000.0); M.My = 0;
  138.   }
  139.   else {
  140.      Ip = SubMgr->Top->Panel->Interior->Yul - Panel->Interior->Yul;
  141.      if (Ip == 0) {
  142.          Rp = 0.0;
  143.      }
  144.      else {
  145.        int Y = Panel->Interior->Ht-1-(Slide->Panel->Overall->Ht-1) / 2;
  146.        Rp = float(Ip)/float(Y);
  147.      }
  148.      M.RtnCode = VtScrollMove;  M.Code = VtScrollMove;
  149.      M.Focus = Base; // Send message to base
  150.      M.My = floor(Rp * 10000.0); M.Mx = 0;
  151.   }
  152.   Base->Dispatch(M);
  153. }
  154.  
  155. void ScrollBar::RcvScrollPosn(float P, BarOrient Which)
  156. // This method takes the number P, between 0.0 and 1.0, and
  157. // cause the Slider to move to the appropriate position 
  158. {
  159.   int NewPos;
  160.   if (Which == HzOrient) {
  161.     // Compute relative position to move slider within slidebar 
  162.     float Rx = Panel->Interior->Wd-1-(Slide->Panel->Overall->Wd-1)/2;
  163.     NewPos = ceil(P*Rx);
  164.     InhibitMessage = True;
  165.     Slide->Move(Panel->Overall->Xul+NewPos,Panel->Overall->Yul);
  166.     InhibitMessage = False;
  167.   }
  168.   else {
  169.     // Compute relative position to move slider within slidebar 
  170.     float Ry = Panel->Interior->Ht-1-(Slide->Panel->Overall->Ht-1)/2;
  171.     NewPos = ceil(P*Ry);
  172.     InhibitMessage = True;
  173.     Slide->Move(Panel->Overall->Xul,NewPos+Panel->Overall->Yul);
  174.     InhibitMessage = False;
  175.   }
  176. }
  177.  
  178. void ScrollBar::OnKeyStroke(MsgPkt &M)
  179. // Sends keystrokes to base window 
  180. {
  181.   Base->OnKeyStroke(M);
  182. }
  183.  
  184. // ------------- ScrollBar Window Type (Swso) ----------- 
  185.  
  186. Swso::Swso(int Ba, int Fa, ColorPak &Cp)
  187. // Initializes a scroll window with two scroll bars 
  188. // and an interior window 
  189. : Wso(Ba, Fa, Cp)
  190. {
  191.   Window = new Wso(Recessed+BorderWd, 0x00, Cp);
  192.   HzSlide = new ScrollBar(HzOrient, Cp);
  193.   VtSlide = new ScrollBar(VtOrient, Cp);
  194. }
  195.  
  196. void Swso::SetSize(int W, int H)
  197. // Set the size of the Swso object and the nested scroll bars. 
  198. // Note: we have to relocate the scroll bars too. SetLocn has
  199. // no way of knowing they need to be relocated if the window
  200. // is resized. 
  201. {
  202.   Wso::SetSize(W, H);
  203.   Window->SetSize(W-Panel->Bwd*2-30, H-Panel->Bwd*2-28);
  204.   HzSlide->SetSize(W-Panel->Bwd*2-30, 9); 
  205.   HzSlide->SetLocn(Panel->Bwd+2, Panel->Interior->Ht-
  206.                    HzSlide->Panel->Interior->Ht-
  207.                    HzSlide->Panel->Bwd*2-Panel->Bwd-2, Relc);
  208.   VtSlide->SetSize(9, H-Panel->Bwd*2-28);
  209.   VtSlide->SetLocn(Panel->Interior->Wd-Panel->Bwd-
  210.                    VtSlide->Panel->Interior->Wd-
  211.                    VtSlide->Panel->Bwd*2-2, Panel->Bwd, Relc);
  212. }
  213.  
  214. void Swso::Open(Iso *B, int X, int Y)
  215. {
  216.   Wso::Open(B, X, Y);
  217.   Window->Open(this, Panel->Bwd+2, Panel->Bwd);
  218.   HzSlide->Open(this, Panel->Bwd+2, Panel->Interior->Ht-
  219.                       HzSlide->Panel->Interior->Ht-
  220.                       HzSlide->Panel->Bwd*2-Panel->Bwd-2);
  221.   VtSlide->Open(this, Panel->Interior->Wd-Panel->Bwd-
  222.                       VtSlide->Panel->Interior->Wd-
  223.                       VtSlide->Panel->Bwd*2-2, Panel->Bwd);
  224. }
  225.  
  226. void Swso::Redraw()
  227. // Redraw the window, interior window, and scroll bars 
  228. {
  229.   Wso::Redraw();
  230.   Window->Redraw();
  231.   HzSlide->Redraw();
  232.   VtSlide->Redraw();
  233. }
  234.  
  235. void Swso::Dispatch(MsgPkt &M)
  236. // Traps messages from the scroll bar and sends them to RcvScrollPosn
  237. // to interpret them 
  238. {
  239.   if (M.Code == HzScrollMove) {
  240.      RcvScrollPosn(float(M.Mx) / 10000.0, HzOrient);
  241.      M.RtnCode = M.Code = Idle;
  242.   }
  243.   else if (M.Code == VtScrollMove) {
  244.      RcvScrollPosn(float(M.My) / 10000.0, VtOrient);
  245.      M.RtnCode = M.Code = Idle;
  246.   }
  247.   else Wso::Dispatch(M);
  248. }
  249.  
  250.  
  251.